﻿using System;
using System.Windows.Forms;

namespace ECP.Utilities.Controls
{
    public partial class UCModuleSelect : DevExpress.XtraEditors.XtraUserControl
    {
        private bool allowNull = true;
        private bool allowSelect;
        private bool multiSelect;
        private bool checkMove;
        public bool SimpleAdmin;

        private BaseUserInfo userInfo = new BaseUserInfo();

        public event BaseInterfaceLogic.SelectedIndexChangedEventHandler SelectedIndexChanged;

        public class SelectModuleInfo
        {
            public string selectedCode = string.Empty;
            public string selectedFullName = string.Empty;
            public string selectedID = string.Empty;
            public string openID = string.Empty;
            public Form form = new Form();
        }

        private SelectModuleInfo selectModule = new SelectModuleInfo();

        public SelectModuleInfo SelectModule
        {
            get { return selectModule; }
            set
            {
                selectModule = value;
                this.txtFullName.Text = value.selectedFullName;
                this.SetControlState();
            }
        }

        public UCModuleSelect()
        {
            InitializeComponent();
            this.SetControlState();
        }

        public bool AllowNull
        {
            get
            {
                return this.allowNull;
            }
            set
            {
                this.allowNull = value;
                this.SetControlState();
            }
        }

        public bool AllowSelect
        {
            get
            {
                return this.allowSelect;
            }
            set
            {
                this.allowSelect = value;
                this.SetControlState();
            }
        }

        public bool CheckMove
        {
            get
            {
                return this.checkMove;
            }
            set
            {
                this.checkMove = value;
            }
        }

        public bool MultiSelect
        {
            get
            {
                return this.multiSelect;
            }
            set
            {
                this.multiSelect = value;
            }
        }

        public BaseUserInfo UserInfo
        {
            get
            {
                this.userInfo = new BaseUserInfo();
                if (!DesignMode)
                {
                    this.userInfo.GetUserInfo();
                }
                return this.userInfo;
            }
            set
            {
                this.userInfo = value;
            }
        }

        public void SetControlState()
        {
            this.pnlNull.Visible = this.AllowNull;
            this.btnSetNull.Visible = this.AllowNull;
            if (string.IsNullOrEmpty(this.SelectModule.selectedID))
            {
                this.btnSetNull.Enabled = false;
            }
            else
            {
                this.btnSetNull.Enabled = true;
            }
            this.btnSelect.Visible = this.AllowSelect;
            this.pnlSelect.Visible = this.AllowSelect;
            if (!string.IsNullOrEmpty(this.SelectModule.selectedFullName))
            {
                this.txtFullName.Text = this.SelectModule.selectedFullName;
            }
            else
            {
                this.txtFullName.Text = string.Empty;
            }
           
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            var form = this.SelectModule.form as IBaseSelectModuleForm;
            if (form == null)
            {
                return;
            }
            var obj = (Form)form.GetType().Assembly.CreateInstance(form.GetType().FullName);

            form = obj as IBaseSelectModuleForm;

            if (!string.IsNullOrEmpty(this.SelectModule.selectedID))
            {
                form.SelectedID = this.SelectModule.selectedID;
            }
            
            if (!string.IsNullOrEmpty(this.SelectModule.openID))
            {
                ((IBaseSelectModuleForm)form).OpenID = this.SelectModule.openID;
            }
            ((IBaseSelectModuleForm)form).CheckMove = this.CheckMove;
            ((IBaseSelectModuleForm)form).AllowNull = this.AllowNull;
            if (obj.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.SelectModule.selectedID = form.SelectedID;
                this.SelectModule.selectedCode = form.SelectedCode;
                this.SelectModule.selectedFullName = form.SelectedFullName;
                if (this.SelectedIndexChanged != null)
                {
                    this.SelectedIndexChanged(this.SelectModule.selectedID);
                }
            }
            this.SetControlState();
        }

        private void btnSetNull_Click(object sender, EventArgs e)
        {
            if (this.txtFullName.Text.Length > 0)
            {
                this.SelectModule.selectedID = null;
                this.SelectModule.selectedCode = null;
                this.SelectModule.selectedFullName = null;
                if (this.SelectedIndexChanged != null)
                {
                    this.SelectedIndexChanged(this.SelectModule.selectedID);
                }
            }
            this.SetControlState();
        }
    }
}